class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        
        if(nums1.size() == 0 || nums2.size() == 0)
            return {};

        int m = nums1.size();
        int n = nums2.size();

        stack<int> s;
        unordered_map<int, int> ans;
        vector<int> res;

        for(int i = 0; i < n; i++)
        {
            while(!s.empty() && s.top() < nums2[i])
            {
                ans[s.top()] = nums2[i];
                s.pop();
            }

            s.push(nums2[i]);
        }

        while(!s.empty())
        {
            ans[s.top()] = -1;
            s.pop();
        }

        for(int j = 0; j < m; j++)
        {
            res.push_back(ans[nums1[j]]);
        }

        return res;
    }
};